/*这个程序是给FPGA输入两个字节的数据，然后读取*/
#include <stdio.h>  
#include <sys/io.h> 
#include <unistd.h>
#define setbit(x,y) x|=(1<<y)
#define clrbit(x,y) x&=~(1<<y)
int BASE=0x378;
void write16(int w) {
 	outb(w & 0xff, BASE+4);
    outb(w >> 8, BASE+4);
}
int read32(void) {
    unsigned char a, b, c, d;

    a = inb(BASE+4);
    b = inb(BASE+4);
    c = inb(BASE+4);
    d = inb(BASE+4);

    return a + (b<<8) + (c<<16) + (d<<24);
}

/**************************************************/
int main(unsigned char data)
{
unsigned char x;
int a,b,c,d,y;
int statusport = BASE + 1;
int controlport = BASE + 2;
unsigned char status,control;
unsigned char ctl;
//char x;
iopl(3);
ioperm(BASE,0x4ff,1);          
unsigned char EPP_ECR = 0x80;
unsigned char old_ECR = inb_p(BASE + 0x402); /* Paused IO for example */
unsigned char low_bits = old_ECR & 0x1f; /* save 5 lowest bits */ 
unsigned char new_ECR = EPP_ECR | low_bits;
outb_p(new_ECR, BASE + 0x402); /* set the port to EPP */

ctl = inb(controlport);
ctl = (ctl &= 0xF0) | 0x4;
outb(ctl, controlport);

ctl= inb(controlport);
setbit(ctl,5);//setbit(ctl,5)是正确的
outb(ctl,controlport);


if ((inb(statusport) & 0x01)) {//if the bit0 ==1,means TIME OUT
  status = inb(statusport); 
  outb(status | 0x01, statusport); /* Some reset by writing 1 */ 
  outb(status & 0xfe, statusport); /* Others by writing 0 */
}

while(1){
/*x=0x00;
outb(x,BASE+3);
y=0x50;
outb(y,BASE+4);
x=0x01;
outb(x,BASE+3);
y=0x00;
outb(y,BASE+4);

x=0x02;
outb(x,BASE+3);
y=0xca;
outb(y,BASE+4);
x=0x03;
outb(x,BASE+3);
y=0x0a;
outb(y,BASE+4);*/

/////////////////////////////
x=0x00;
outb(x,BASE+3);
x=0x00;
outb(x,BASE+3);
x=0x00;
outb(x,BASE+3);
a=inb_p(BASE+4);
usleep(100);
b=inb_p(BASE+4);
usleep(100);
c=inb_p(BASE+4);
usleep(100);
d=inb_p(BASE+4);
usleep(100);
printf("a=%x\n",a);
printf("b=%x\n",b);
printf("c=%x\n",c);
printf("d=%x\n",d);

usleep(1000);
}

x=inb(BASE+4);
printf("x=%x\n",x);

ioperm(BASE,0x4ff,0);  
return 0;
}  
